﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  StorageItemMonitor.cs
//    
// Abstract:
//
//  This module implements the StorageItemMonitor class. This class monitors for changes to the storage item.
//
//--

using System;
using System.Diagnostics;
using Microsoft.OfficeCommunicationsServer.Applications.Common;

namespace Microsoft.EnhancedReminders.Scheduler
{
    /// <summary>
    /// This class monitors for changes to the storage item.
    /// </summary>
    internal class StorageItemMonitor : Monitor
    {
        // Name of the search folder. A GUID is used to reduce the likelihood that the name is already in use. A constant
        // value is used so that any orphaned folders from the previous instance of the Scheduler service can be deleted 
        // when the service starts.
        private const string _storageItemSearchFolderName = "5C0C90D71FB4455b98B0F5BA32A443AA";

        // Notification events of interest to this monitor. For storage items, all events are of interest.
        private static NotificationEventTypeType[] _events = new NotificationEventTypeType[] {
            NotificationEventTypeType.NewMailEvent,  NotificationEventTypeType.CopiedEvent, NotificationEventTypeType.CreatedEvent, 
            NotificationEventTypeType.DeletedEvent,  NotificationEventTypeType.ModifiedEvent, NotificationEventTypeType.MovedEvent };

        /// <summary>
        /// ctor.
        /// </summary>
        /// <param name="client">WCF proxy for communicating with EWS</param>
        /// <param name="impersonation">Exchange impersonation info</param>
        /// <param name="emailAddress">Mailbox account to be monitored</param>
        /// <param name="taskSerializer">Task serializer</param>
        internal StorageItemMonitor(ExchangeServicePortType client, ExchangeImpersonationType impersonation, string emailAddress, 
                TaskSerializer taskSerializer)

            : base(client, impersonation, emailAddress, _storageItemSearchFolderName,
                ExchangeHelpers.GetDistinguishedFolder(DistinguishedFolderIdNameType.root, emailAddress), 
                ExchangeHelpers.GetDistinguishedFolder(DistinguishedFolderIdNameType.inbox, emailAddress),
                SearchFolderTraversalType.Deep, _events, taskSerializer)
        {
        }

        /// <summary>
        /// Gets the specified storage item.
        /// </summary>
        /// <param name="taskContext">Task context</param>
        /// <param name="itemId">Storage item to be retrieved</param>
        protected override void GetItem(TaskContext taskContext, ItemIdType itemId)
        {
            Debug.Assert(taskContext != null, "taskContext != null");
            Debug.Assert(itemId != null, "itemId != null");

            // Notifications from EWS need to be handled on the task serializer to ensure that the tasks are handled in the correct order.
            GetStorageItemTask getStorageItemTask = new GetStorageItemTask(taskContext, _client, _impersonation, _emailAddress, itemId);
            getStorageItemTask.Completed += GetItem_Completed;
            _taskSerializer.Execute(getStorageItemTask);
        }
    }
}
